package cate.game.db.mongo;

import cate.game.role.base.RoleBase;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

import java.util.ArrayList;
import java.util.List;

public interface RoleBaseDao extends MongoRepository<RoleBase, String> {
    @Query("{'name': ?0}")
    Page<RoleBase> findByName(String name, Pageable page);

    default List<RoleBase> searchByName(String name, int limitNum) {
        List<RoleBase> list = new ArrayList<>();
        Page<RoleBase> page = findByName(name, Pageable.ofSize(limitNum));
        if (page != null && !page.isEmpty()) {
            for (RoleBase roleBase : page) {
                list.add(roleBase);
            }
        }
        return list;
    }
}
